package com.afd.ozblip.net;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import java.io.IOException;
import java.util.Map;

import com.afd.ozblip.util.PhoneUtil;




/**
 * HTTP工具类
 * 
 * @author 徐鹏
 * @version 1.0
 */
public class HttpUtil {
	public static final String TAG = "Utils";
	public static final long DAY_IN_MILLIS = 86400000L;
	public static final long HOUR_IN_MILLIS = 3600000L;
	public static final long MINUTE_IN_MILLIS = 60000L;
	public static final long SECOND_IN_MILLIS = 1000L;

	/**
	 * 判断是否是CMWAP网络
	 * 
	 * @param paramContext
	 * @return
	 */
	private static boolean isCMWAP(Context paramContext) {
		// 获取手机当前网络信息
		NetworkInfo netWorkInfo = ((ConnectivityManager) paramContext
				.getSystemService("connectivity")).getActiveNetworkInfo();
		if ((netWorkInfo == null) || (!netWorkInfo.isAvailable())) {
			return false;
		}
		if (netWorkInfo.getTypeName().equals("WIFI")) {
			return false;
		}
		if ((netWorkInfo.getTypeName().equals("MOBILE"))
				&& ("cmwap".equals(netWorkInfo.getExtraInfo().toLowerCase()))) {
			return true;
		}
		if ((netWorkInfo.getTypeName().equals("MOBILE"))
				&& ("ctwap".equals(netWorkInfo.getExtraInfo().toLowerCase()))) {
			return true;
		}
		return false;
	}

	/**
	 * 通过Post方式请求数据
	 * 
	 * @param paramContext
	 *            当前文档
	 * @param paramHandler
	 *            handler对象
	 * @param postUrl
	 *            提交URL
	 * @param paramMap
	 *            参数Map集合
	 */
	public static byte[] requestByPost(Context paramContext,String postUrl, Map<String, Object> paramMap) {
		byte[] bData = null;
		// 如果没有网络则不更新UI
		if (!PhoneUtil.hasNetwork(paramContext)) {
			return bData;
		}
		try {
			HttpTransport htPost = new HttpTransport();
			bData = htPost.post(isCMWAP(paramContext), postUrl, paramMap);
			System.out.println("返回的数据：" + new String(bData, "UTF-8"));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bData;
	}

	/**
	 * 通过Get方式请求数据
	 * 
	 * @param paramContext
	 *            当前文档
	 * @param paramHandler
	 *            handler对象
	 * @param getUrl
	 *            提交URL
	 * @param paramMap
	 *            参数Map集合
	 */
	public static byte[] requestByGet(Context paramContext,String getUrl, Map<String, Object> paramMap) {
		byte[] bData = null;
		// 如果没有网络则不更新UI
		if (!PhoneUtil.hasNetwork(paramContext)) {
			return bData;
		}
		try {
			HttpTransport htGet = new HttpTransport();
			bData = htGet.get(isCMWAP(paramContext), getUrl, paramMap);
			System.out.println("返回的数据：" + new String(bData, "UTF-8"));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bData;
	}
	/**
	 * 通过Get方式请求数据
	 * 
	 * @param paramContext
	 *            当前文档
	 * @param paramHandler
	 *            handler对象
	 * @param getUrl
	 *            提交URL
	 * @param paramMap
	 *            参数Map集合
	 */
	public static byte[] requestByGet(Context paramContext,String getUrl) {
		byte[] bData = null;
		// 如果没有网络则不更新UI
		if (!PhoneUtil.hasNetwork(paramContext)) {
			return bData;
		}
		try {
			HttpTransport htGet = new HttpTransport();
			bData = htGet.get(isCMWAP(paramContext), getUrl);
			System.out.println("返回的数据：" + new String(bData, "UTF-8"));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bData;
	}

}